package com.sz.mes.controller.warehouse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sz.mes.mapper.warehouse.AreaMapper;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.diboot.core.vo.*;
import com.diboot.iam.annotation.OperationCons;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.Log;
import com.sz.mes.entity.warehouse.Area;
import com.sz.mes.dto.warehouse.AreaDTO;
import com.sz.mes.vo.warehouse.AreaListVO;
import com.sz.mes.vo.warehouse.AreaDetailVO;
import com.sz.mes.service.warehouse.AreaService;
import com.sz.mes.controller.BaseCustomCrudRestController;

import lombok.extern.slf4j.Slf4j;

import javax.validation.Valid;
import java.util.List;

/**
 * 仓库货区 相关Controller
 *
 * @author hem
 * @version 1.0
 * @date 2023-05-25
 * Copyright © szhs
 */
@RestController
@RequestMapping("/warehouse/area")
@BindPermission(name = "仓库货区", code = "WarehouseArea")
@Slf4j
public class AreaController extends BaseCustomCrudRestController<Area> {
    @Autowired
    private AreaService areaService;

    @Autowired
    private AreaMapper areaMapper;

    /**
     * 查询ViewObject的分页数据
     * <p>
     * url请求参数示例: /list?field=abc&pageIndex=1&orderBy=abc:DESC
     * </p>
     *
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_LIST)
//    @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping("/list")
    public JsonResult getViewObjectListMapping(AreaDTO queryDto, Pagination pagination) throws Exception {
        return super.getViewObjectList(queryDto, pagination, AreaListVO.class);
    }

    /**
     * 根据资源id查询ViewObject
     *
     * @param id ID
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DETAIL)
//    @BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
    @GetMapping("/{id}")
    public JsonResult getViewObjectMapping(@PathVariable("id") Long id) throws Exception {
        return super.getViewObject(id, AreaDetailVO.class);
    }

    /**
     * 创建资源对象
     *
     * @param entity
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_CREATE)
//    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/")
    public JsonResult createEntityMapping(@Valid @RequestBody Area entity) throws Exception {
        try {
            //根据仓库id跟货区名称确定唯一
            String areaName = entity.getAreaName();
            Long id = entity.getId();
            QueryWrapper<Area> areaQueryWrapper = new QueryWrapper<>();
            areaQueryWrapper.eq("area_name", areaName);
            areaQueryWrapper.eq("warehouse_id", id);
            Area area = areaMapper.selectOne(areaQueryWrapper);
            if (area == null) {
                return super.createEntity(entity);
            } else {
                return JsonResult.FAIL_EXCEPTION("该货区名称已存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.FAIL_EXCEPTION("该货区名称已存在");
        }
    }

    /**
     * 根据ID更新资源对象
     *
     * @param entity
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
//    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/{id}")
    public JsonResult updateEntityMapping(@PathVariable("id") Long id, @Valid @RequestBody Area entity) throws Exception {
        try {
            //根据仓库id跟货区名称确定唯一
            String areaName = entity.getAreaName();
            Long warehouseId = entity.getWarehouseId();
            QueryWrapper<Area> areaQueryWrapper = new QueryWrapper<>();
            areaQueryWrapper.eq("area_name", areaName);
            areaQueryWrapper.eq("warehouse_id", warehouseId);
            Area area = areaMapper.selectOne(areaQueryWrapper);
            if (area == null) {
                return super.updateEntity(id, entity);
            } else {
                return JsonResult.FAIL_EXCEPTION("货区名称、编号不能重复");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.FAIL_EXCEPTION("货区名称、编号不能重复");
        }
    }

    /**
     * 根据id删除资源对象
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DELETE)
//    @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @DeleteMapping("/{id}")
    public JsonResult deleteEntityMapping(@PathVariable("id") Long id) throws Exception {
//        return super.deleteEntity(id);
        return areaService.deleteById(id);
    }


}
